//
//  TLScrollPlayCell.m
//  TLAVPlayerDemo
//
//  Created by Tianlong on 2017/7/8.
//  Copyright © 2017年 Tianlong. All rights reserved.
//

#import "TLScrollPlayCell.h"

#import "TLAVPlayerPublicFile.h"


@interface TLScrollPlayCell ()<TLPlayerDelegate>
@property (nonatomic , weak) UILabel *title;
@end

@implementation TLScrollPlayCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

/**
 重载方法来定义根据传递的模型定义自己的标识符
 */
+(instancetype)cellWithTableView:(UITableView *)tableView model:(id)model cellStyle:(TLTableViewCellStyle)cellStyle{
    
    NSString *cellId = NSStringFromClass([TLScrollPlayCell class]);
    TLScrollPlayCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[TLScrollPlayCell alloc] initWithStyle:[TLScrollPlayCell getTableCellStyleWithTLTableCellStyle:cellStyle] reuseIdentifier:cellId model:model];
    }
    else{
        //删除内部的播放视图
        for (UIView *sub in cell.contentView.subviews) {
            if (sub.subviews.count > 0) {
                for (UIView *sub2 in sub.subviews) {
                    if ([sub2 isKindOfClass:[TLAVPlayer class]]) {
                        TLAVPlayer *player = (TLAVPlayer *)sub2;
                        //清除之前的playerlayer
                        [player.playerLayer removeFromSuperlayer];
                        player.playerLayer = nil;
                        NSLog(@"删除重用的playerLayer ==  %ld",(long)cell.playModel.index);
                    }
                    [sub2 removeFromSuperview];
                }
            }
            [sub removeFromSuperview];
        }
        TLScrollPlayModel *playModel = nil;
        if ([model isKindOfClass:[TLScrollPlayModel class]]) {
            playModel = (TLScrollPlayModel *)model;
        }
        [cell resetSubviewWithPlayModel:playModel];
    }
    return cell;
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier model:(id)model{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.identifier = reuseIdentifier;
        self.model = model;
        
        if ([model isKindOfClass:[TLScrollPlayModel class]]) {
            _playModel = (TLScrollPlayModel *)self.model;
        }
        [self resetSubviewWithPlayModel:self.playModel];
    }
    return self;
}

-(void)resetSubviewWithPlayModel:(TLScrollPlayModel *)playModel{
    //添加标题
    UILabel *title = [[UILabel alloc] init];
    title.textColor = [UIColor blackColor];
    title.numberOfLines = 0;
    title.text = playModel.videoURL.length > 0 ? playModel.videoURL : @"视频地址为空";
    [self.contentView addSubview:title];
    self.title = title;
    
    CGFloat margin = 15;
    WS(ws);
    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ws.contentView.mas_top).offset(margin);
        make.left.equalTo(ws.contentView.mas_left).offset(margin);
        make.right.equalTo(ws.contentView.mas_right).offset(-margin);
        //make.height.offset(20);
    }];
    
    //添加播放器
    //playModel.player.backgroundColor = RandomColor;
    
    UIView *middView = [[UIView alloc] init];
    self.middView = middView;
    self.middView.backgroundColor = RandomColor;
    [self.contentView addSubview:self.middView];
    [self.middView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ws.title.mas_bottom).offset(margin);
        make.left.equalTo(ws.contentView.mas_left).offset(margin);
        make.right.equalTo(ws.contentView.mas_right).offset(-margin);
        make.height.offset(220);
        make.bottom.equalTo(ws.contentView.mas_bottom).offset(-margin*2);
    }];
    
    if (playModel.videoURL.length > 0 && [playModel.videoURL containsString:@"http"]) {
        [self.middView addSubview:playModel.player];
        [playModel.player mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(ws.middView.mas_top).offset(margin);
            make.left.equalTo(ws.middView.mas_left).offset(margin);
            make.right.equalTo(ws.middView.mas_right).offset(-margin);
            //make.height.offset(220);
            make.bottom.equalTo(ws.middView.mas_bottom).offset(-margin);
        }];
        playModel.player.backgroundColor = RandomColor;
    }
}


-(void)setPlayModel:(TLScrollPlayModel *)playModel{
    _playModel = playModel;
    
}

@end

/**
 //        TLAVPlayer *player = [[TLAVPlayer alloc] init];
 //        player.beginTime = 0;
 //        player.autoPlay = NO;
 //        player.showBackBtnInSmallScreen = NO;
 //        player.coverView.hidden = NO;
 //        self.player = player;
 //        [self addSubview:self.player];
 */
